<template>
  <div class="listpep">
    <div class="soger">
      <el-input placeholder="请输入内容" v-model="input" clearable></el-input>
      <div class="tu">
        <img src="../../../assets/wechatimgs/search.png" alt="" />
      </div>
    </div>
    <div class="list">
      <ul>
        <li
          class="contact-item"
          v-for="(item, index) in filteredList"
          :key="index"
          @click="handleSelectUser(item)"
        >
          <div class="avatar">
            <img src="../../../assets/wechatimgs/yonghu.png" alt="" />
          </div>
          <div class="info">
            <div class="name">
              {{ item.name }}
              <div class="wh">你好啊！</div>
            </div>
            <div class="gd">...</div>
            <div class="divider"></div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "ListPep",
  data() {
    return {
      input: "",
      originalList: [], // 存储原始数据
    };
  },
  computed: {
    //filter筛选数据
    filteredList() {
      const keyword = this.input.trim().toLowerCase();
      if (!keyword) return this.originalList;
      return this.originalList.filter((item) => {
        return item.name.toLowerCase().includes(keyword);
      });
    },
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      try {
        const response = await axios.get(
          "http://websocket.1uu.pw/user/page?pageSize=999"
        );
        this.originalList = response.data.data.records; // 保存原始数据
      } catch (error) {
        console.error("获取数据失败:", error);
      }
    },
    handleSelectUser(user) {
      this.$store.commit("SET_SELECTED_USER", user);
    },
  },
};
</script>

<style lang="scss" scoped>
.listpep {
  width: 100%;
  height: 100%;
  .soger {
    width: 100%;
    height: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #fff;
    position: relative;
    border: 1px solid #000;

    .el-input {
      width: 80%;
      border-radius: 10px;
    }
    ::v-deep .el-input__inner {
      background-color: #f0f0f0;
    }

    .tu {
      position: absolute;
      left: 10%;
      top: 30%;
    }
  }
  .list {
    width: 100%;
    height: calc(100% - 60px);
    background-color: #fff;
    border-bottom: 1px solid #000;
    overflow: auto;

    ul {
      background: #fff;
      padding-left: 16px;
      overflow-y: auto;
    }

    .contact-item {
      display: flex;
      align-items: center;
      height: 80px;
      position: relative;

      .avatar {
        width: 40px;
        height: 40px;
        border-radius: 6px;
        background: #e5e5e5;
        margin-right: 12px;

        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }

      .name {
        font-size: 20px;
        color: #333;
      }

      .wh {
        font-size: 15px;
      }

      .gd {
        position: absolute;
        right: 0;
        font-size: 25px;
        top: 10px;
      }

      .divider {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        height: 1px;
        background: #e5e5e5;
        margin-left: 56px;
      }

      &:last-child .divider {
        display: none;
      }
    }
  }
}
</style>
